package com.fx.ruankao.service.impl;

import com.fx.ruankao.entity.ExamRecord;
import com.fx.ruankao.mapper.ExamRecordMapper;
import com.fx.ruankao.service.ExamRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 考试记录服务实现类
 */
@Service
@Transactional
public class ExamRecordServiceImpl implements ExamRecordService {
    
    @Autowired
    private ExamRecordMapper examRecordMapper;
    
    @Override
    public int insert(ExamRecord examRecord) {
        // 默认状态为未开始
        if (examRecord.getStatus() == null) {
            examRecord.setStatus(0);
        }
        return examRecordMapper.insert(examRecord);
    }
    
    @Override
    public int deleteById(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("考试记录ID不能为空且必须大于0");
        }
        return examRecordMapper.deleteById(id);
    }
    
    @Override
    public int update(ExamRecord examRecord) {
        return examRecordMapper.update(examRecord);
    }
    
    @Override
    public ExamRecord selectById(Long id) {
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("考试记录ID不能为空且必须大于0");
        }
        return examRecordMapper.selectById(id);
    }
    
    @Override
    public List<ExamRecord> selectByUserId(Long userId) {
        if (userId == null || userId <= 0) {
            throw new IllegalArgumentException("用户ID不能为空且必须大于0");
        }
        ExamRecord examRecord = new ExamRecord();
        examRecord.setUserId(userId);
        return examRecordMapper.selectList(examRecord);
    }
    
    @Override
    public List<ExamRecord> selectByPaperId(Long paperId) {
        if (paperId == null || paperId <= 0) {
            throw new IllegalArgumentException("试卷ID不能为空且必须大于0");
        }
        ExamRecord examRecord = new ExamRecord();
        examRecord.setPaperId(paperId);
        return examRecordMapper.selectList(examRecord);
    }
    
    @Override
    public ExamRecord selectByUserIdAndPaperId(Long userId, Long paperId) {
        if (userId == null || userId <= 0) {
            throw new IllegalArgumentException("用户ID不能为空且必须大于0");
        }
        if (paperId == null || paperId <= 0) {
            throw new IllegalArgumentException("试卷ID不能为空且必须大于0");
        }
        return examRecordMapper.selectByUserIdAndPaperId(userId, paperId);
    }
    
    @Override
    public int batchDelete(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new IllegalArgumentException("请至少选择一个要删除的考试记录");
        }
        // ExamRecordMapper接口中没有batchDelete方法，这里通过循环删除实现
        int count = 0;
        for (Long id : ids) {
            if (id != null && id > 0) {
                count += examRecordMapper.deleteById(id);
            }
        }
        return count;
    }
    
    @Override
    public boolean updateScore(Long recordId, Integer score) {
        if (recordId == null || recordId <= 0) {
            throw new IllegalArgumentException("考试记录ID不能为空且必须大于0");
        }
        if (score == null || score < 0) {
            throw new IllegalArgumentException("成绩不能为负数");
        }
        
        ExamRecord examRecord = new ExamRecord();
        examRecord.setId(recordId);
        examRecord.setTotalScore(score.doubleValue());
        examRecord.setStatus(2); // 设置为已完成状态
        return examRecordMapper.update(examRecord) > 0;
    }
    
    @Override
    public boolean updateStatus(Long recordId, Integer status) {
        if (recordId == null || recordId <= 0) {
            throw new IllegalArgumentException("考试记录ID不能为空且必须大于0");
        }
        if (status == null || (status != 0 && status != 1 && status != 2)) {
            throw new IllegalArgumentException("状态值无效，只能为0(未开始)、1(进行中)、2(已完成)");
        }
        
        ExamRecord examRecord = new ExamRecord();
        examRecord.setId(recordId);
        examRecord.setStatus(status);
        
        // 设置状态变更时间
        if (status == 1) { // 开始考试
            examRecord.setStartTime(new java.util.Date());
        } else if (status == 2) { // 完成考试
            examRecord.setEndTime(new java.util.Date());
        }
        
        return examRecordMapper.update(examRecord) > 0;
    }
    
    @Override
    public int countRecords() {
        return examRecordMapper.selectCount(new ExamRecord());
    }
    
    @Override
    public int countRecordsByCondition(Long userId, Long paperId, Integer status) {
        ExamRecord examRecord = new ExamRecord();
        
        if (userId != null && userId > 0) {
            examRecord.setUserId(userId);
        }
        if (paperId != null && paperId > 0) {
            examRecord.setPaperId(paperId);
        }
        examRecord.setStatus(status);
        
        return examRecordMapper.selectCount(examRecord);
    }
}